package com.mashibing.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

/**
 * Copyright (C) 2024 - 2024 Jasonakeke， Inc. All Rights Reserved
 *
 * @Desc :
 * @Time : 2024/10/2 16:16
 * @Author : Code_By_Jasonakeke
 * @Email : 2284037977@qq.com
 * @Class : TankFrame
 * @IDE : IntelliJ IDEA
 */
public class TankFrame extends Frame {

	private Tank myTank = new Tank(200, 400, Dir.DOWN, Group.GOOD, this);


	public List<Bullet> bullets = new ArrayList<>();


	public List<Tank> tanks = new ArrayList<>();
	List<Explode> explodes = new ArrayList<>();
	Explode explode = new Explode(100, 100, this);

	public static final int GAME_WIDTH = Integer.parseInt((String) PropertyMgr.get("gameWidth"));

	public static final int GAME_HEIGHT = Integer.parseInt((String) PropertyMgr.get("gameHeight"));


	public Tank getMyTank() {
		return myTank;
	}

	public void setMyTank(Tank myTank) {
		this.myTank = myTank;
	}


	public Image getOffScreenImage() {
		return offScreenImage;
	}

	public void setOffScreenImage(Image offScreenImage) {
		this.offScreenImage = offScreenImage;
	}

	public TankFrame() {
		setSize(GAME_WIDTH, GAME_HEIGHT);
		setResizable(true);
		setTitle("tank war");
		setVisible(true);
		addKeyListener(new MyKeyListener());
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	private Image offScreenImage = null;

	@Override
	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.BLACK);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	@Override
	public void paint(Graphics g) {
		Color color = g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("子弹的数量：" + bullets.size(), 10, 60);
		g.drawString("敌人的数量：" + tanks.size(), 10, 80);
		g.drawString("爆炸的数量：" + explodes.size(), 10, 100);
		g.setColor(color);
		myTank.paint(g);
		for (int i = 0; i < bullets.size(); i++) {
			bullets.get(i).paint(g);
		}
		for (int i = 0; i < tanks.size(); i++) {
			tanks.get(i).paint(g);
		}

		for (int i = 0; i < explodes.size(); i++) {
			explodes.get(i).paint(g);
		}

		// coillision detect
		for (int i = 0; i < bullets.size(); i++) {
			for (int j = 0; j < tanks.size(); j++) {
				bullets.get(i).collideWith(tanks.get(j));
			}
		}

		// for (Iterator<Bullet> iterator = bullets.iterator(); iterator.hasNext(); ) {
		// 	Bullet bullet = iterator.next();
		// 	bullet.paint(g);
		// 	if (!bullet.isLive()){
		// 		iterator.remove();
		// 	}
		// }
	}

	public List<Bullet> getBullets() {
		return bullets;
	}

	public void setBullets(List<Bullet> bullets) {
		this.bullets = bullets;
	}

	class MyKeyListener extends KeyAdapter {

		private boolean bLeft = false;

		private boolean bUp = false;

		private boolean bRight = false;

		private boolean bDown = false;


		@Override
		public void keyPressed(KeyEvent e) {
			int key = e.getKeyCode();
			switch (key) {
				case KeyEvent.VK_LEFT:
					bLeft = true;
					break;
				case KeyEvent.VK_RIGHT:
					bRight = true;
					break;
				case KeyEvent.VK_UP:
					bUp = true;
					break;
				case KeyEvent.VK_DOWN:
					bDown = true;
					break;
				case KeyEvent.VK_CONTROL:
					myTank.fire();
					break;
				default:
					break;
			}
			setMainTankDir();
		}

		private void setMainTankDir() {
			if (!bLeft && !bRight && !bUp && !bDown) {
				myTank.setMoving(false);
			} else {
				myTank.setMoving(true);
				if (bLeft) {
					myTank.setDir(Dir.LEFT);
				}
				if (bRight) {
					myTank.setDir(Dir.RIGHT);
				}
				if (bUp) {
					myTank.setDir(Dir.UP);
				}
				if (bDown) {
					myTank.setDir(Dir.DOWN);
				}
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			int keyCode = e.getKeyCode();
			switch (keyCode) {
				case KeyEvent.VK_LEFT:
					bLeft = false;
					break;
				case KeyEvent.VK_RIGHT:
					bRight = false;
					break;
				case KeyEvent.VK_UP:
					bUp = false;
					break;
				case KeyEvent.VK_DOWN:
					bDown = false;
					break;
				default:
					break;
			}
			setMainTankDir();
		}
	}
}
